import axios from "axios";
import { MessagePlugin } from "tdesign-vue-next";
const http = (config) => {
  const request = axios.create({
    // baseURL: "/api",
    baseURL: "http://zhongapi.yjinzi.com/",
    timeout: 50000,
    headers: {},
  });
  // 请求拦截器
  request.interceptors.request.use(
    (config) => {
      return config;
    },
    (err) => {
      return Promise.reject(err);
    }
  );
  // 响应拦截器
  request.interceptors.response.use(
    (res) => {
      const { status, statusText, data } = res;
      if (status === 200) {
        if (data.code === 0) {
          // 返回数据
          return data.data;
        } else {
          MessagePlugin.error(data.message);
          return Promise.reject(data);
        }
      } else {
        MessagePlugin.error(statusText);
        return Promise.reject(statusText);
      }
    },
    (err) => {
      MessagePlugin.error(err.message);
      return Promise.reject(err);
    }
  );
  return request(config);
};

/**
 * GET 请求
 * @param {*} url
 * @param {*} params
 */
export const httpGet = (url, params) => {
  return http({
    url,
    method: "GET",
    params,
  });
};
/**
 * POST 请求
 * @param {*} url
 * @param {*} params
 */
export const httpPost = (url, data) => {
  return http({
    url,
    method: "POST",
    data,
  });
};

export default http;
